package com.eascs.web.p2p.loginAction;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.eascs.app.dao.PageResult;
import com.eascs.p2p.common.entity.AdminResourceEntity;
import com.eascs.p2p.common.entity.AdminRoleEntity;
import com.eascs.p2p.common.service.IAdminResourceService;
import com.eascs.p2p.common.service.IAdminRoleService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.p2p.common.vo.UserRoleVo;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.tag.MenuTag;

@Controller
@RequestMapping("/act/role")
public class RoleController extends PermissionBaseMasterViewController {
	
	@Reference
	private IAdminResourceService iAdminResourceService;
	@Reference
	private IAdminRoleService  iAdminRoleService;

	final static int PAGE_SIZE = 15;
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "admin");
	}
	/**
	 * @Title: detail
	 * @Description:角色对应的资源详情
	 * @author lwx
	 * @param 角色id
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping("/detail-{id}")
	public String detail(Model model, @PathVariable String id) {
		AdminRoleEntity  role = new AdminRoleEntity();
		if (id != null) {
			role = iAdminRoleService.getAdminRoleById(id).getModel();
		}
		//默认资源和选中资源
		List<AdminRoleEntity> roles=new ArrayList<>();
		roles.add(role);
		List<AdminResourceEntity> myList = iAdminResourceService.selectResourcesByRoles(roles).getModel();
		List<AdminResourceEntity> myList1 = iAdminResourceService.selectDefaultResourcesList().getModel();
		if(myList!=null){
			myList.addAll(myList1);
		}
		//资源列表
		List<AdminResourceEntity> allList = iAdminResourceService.selectResourcesList().getModel();
		model.addAttribute("allResList", JSON.toJSONString(allList));
		model.addAttribute("myResList", JSON.toJSONString(myList));
		model.addAttribute("role", role);
		return "sys/roleInfo";
	}
	
	/**
	 * @Title: addRole
	 * @Description:添加角色
	 * @author lwx
	 * @param 角色id
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping("/addRole")
	public String addRole() {
		return "sys/addRole";
	}
	
	
	/**
	 * @Title: search
	 * @Description:角色列表
	 * @author lwx
	 * @param UserRoleVo
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping("/list")
	public String search(UserRoleVo vo) {
		vo.setPageSize(PAGE_SIZE);
		PageResult<Object[]> pageResult = null;
		if (-1 == vo.getShowPage()) {
			pageResult = new PageResult<>(10, 0, 0);
		} else {
			ModelResult<PageResult<Object[]>> modelResult = iAdminRoleService.selectRolePage(vo);
			pageResult = modelResult.getModel();
		}
		String[] titles = { "角色编码", "角色名称", "角色描述" };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, "/act/role/list?pageIndex=");
		Button[] titleButton = new Button[] { new RedirectButton("权限设置", "/act/role/detail-"),
				                              new AjaxButton("删除", "/act/role/rest/delete-") };
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setMoreButtons(titleButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);
		return "/sys/roleList";
	}

}
